package com.joysuch.wwyt.workflow.api.service.impl;

import com.joysuch.wwyt.api.dataaccess.DataAccessService;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.workflow.api.service.WorkFlowAPIService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class WorkFlowAPIServiceImpl implements WorkFlowAPIService {

    @Autowired
    private BaseConfigService baseConfigService;

    private final static String   WWYT_API_SEND_DATA_OPEN ="wwyt.api.send.data.open";

    @Autowired
    private DataAccessService dataAccessService;

    @Override
    public void sendData(String formData, String taskName, String formKeyLabelJson, HttpServletRequest request) {
        Long tenentId = Context.getCurrentTenantId();
        String companyCode = Context.getCompanyCode();
        BaseConfig apiConfig = baseConfigService.findConfig(tenentId,companyCode, WWYT_API_SEND_DATA_OPEN);
        boolean isOpen = false;
        // 判断API 接口开关是否打开
        if(apiConfig != null && StringUtils.isNotEmpty(apiConfig.getValue()) && "true".equalsIgnoreCase(apiConfig.getValue())){
            isOpen = true;
        }
        // 如果接口开关关闭，则直接返回，不执行后续逻辑
        if(!isOpen){
           return ;
        }

        if("隐患核销".equals(taskName)){
            dataAccessService.hiddenDangerReport(formData,formKeyLabelJson);
        }else if ("提交投入".equals(taskName)){
            dataAccessService.safeInput(formData,formKeyLabelJson);
        }else if ("文档发布".equals(taskName)){
            dataAccessService.lawStandardReport(formData,formKeyLabelJson);
        }else if ("提交值班记录".equals(taskName)){
            dataAccessService.leaderDutyTable(formData,formKeyLabelJson);
        }else if ("提交带班值班记录".equals(taskName)){
            dataAccessService.leaderDutyRecord(formData,formKeyLabelJson);
        }else if ("提交风险四色图".equals(taskName)) {
            dataAccessService.riskDistrict(formData, formKeyLabelJson);
        }else if("变更验收".equals(taskName)){
            dataAccessService.changeApply(formData,formKeyLabelJson);
        }
    }
}
